package com.ruiaa.meeting.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.ruiaa.meeting.model.Api;
import lombok.Getter;

import java.util.List;

/**
 * @author chenhuaiyi
 * @date 2022-11-04 10:01
 */
@Getter
//序列化成Json字符串的时候将NULL值去除
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CommonResponse<T> {

    private final Integer code;
    private final String message;
    private final T data;
    private List<Api> apis;

    public CommonResponse(Integer code, String message, T data){
        this.code = code;
        this.message = message;
        this.data = data;
        this.apis = null;
    }

    public void setApis(List<Api> apis) {
        this.apis = apis;
    }

    /**
     * 返回成功信息，包含返回数据和API
     * @param data 返回数据
     * @param apis 用户接下来可能会用到的API
     */
    public static <T> CommonResponse<T> createForSuccessIncludeApis(T data, List<Api> apis){
        CommonResponse<T> success = new CommonResponse<>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMessage(), data);
        success.setApis(apis);
        return success;
    }

    /**
     * 返回成功信息，无返回数据
     */
    public static <T> CommonResponse<T> createForSuccess(){
        return new CommonResponse<>(ResponseCode.SUCCESS.getCode(),ResponseCode.SUCCESS.getMessage(),null);
    }


    /**
     * 返回成功信息，只包含返回数据
     * @param data 返回数据
     */
    public static <T> CommonResponse<T> createForSuccess(T data){
        return new CommonResponse<>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getMessage(), data);
    }

    /**
     * 返回错误信息，包括错误提示和API
     * @param message 错误消息
     * @param apis 接下来可能会用到的API
     */
    public static <T> CommonResponse<T> createForErrorIncludeApi(String message, List<Api> apis){
        CommonResponse<T> error = new CommonResponse<>(ResponseCode.ERROR.getCode(), message, null);
        error.setApis(apis);
        return error;
    }

    /**
     * 返回错误信息，只包括错误提示
     * @param message 错误提示
     */
    public static <T> CommonResponse<T> createForError(Integer code, String message){
        return new CommonResponse<>(code, message, null);
    }

    /**
     *
     * @param message 错误信息
     */
    public static <T> CommonResponse<T> createForError(String message){
        return new CommonResponse<>(ResponseCode.ERROR.getCode(), message, null);
    }

}
